<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
   <script>
    // 2.编写函数getobj(arr) 统计数字出现的次数。
        /* 
           function getobj(arr){
                return obj; // {a:2,b:2,c:1}
            }
            var arr  = ["a","a","b","c","b"];
        */

    //     var arr  = ["a","a","b","c","b"];
    //     getobj(arr);

    var arr  = ["a","a","b","c","b"];
    getobj(arr);
       function getobj(arr){
           var obj = {};
            for(var i=0;i<arr.length;i++){
                // console.log(arr[i]);
                // {a:1,a:1,b:1,c:1,b:1}  对象的键名不会重复
                if(typeof obj[arr[i]] == "undefined"){
                    // 第一次循环 进来  a ---> obj['a'] == undefined
                    obj[arr[i]] = 1;
                }else{
                    // console.log("第二次相同的键名进入else",arr[i]);
                    // obj['a'] + 1
                    obj[arr[i]]++;
                }
            }
            // console.log(obj);
            return obj; // {a:2,b:2,c:1}
        }
        // console.log(obj);
        console.log(getobj(arr));
    
   </script> 
</body>
</html>